package mulMan.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class PlaylistModel extends Observable implements Observer{
	
	private Playlist playlist;
	private List<AudioFileModel> audioFilesModels;
	
	public PlaylistModel() {
		playlist = new Playlist();
		playlist.addObserver(this);
		audioFilesModels = new ArrayList<AudioFileModel>();
	}
	

	public Playlist getPlaylist() {
		return playlist;
	}
	public void setPlaylist(Playlist playlist) {
		this.playlist = playlist;
		playlist.addObserver(this);
		
		audioFilesModels = new ArrayList<AudioFileModel>();
		
		for (AudioFile iterator : playlist.getAudioFiles()) {
			AudioFileModel audioFileModel = new AudioFileModel();
			audioFileModel.setAudioFile(iterator);
			audioFilesModels.add(audioFileModel);
		}
		
		setChanged();
		notifyObservers("all");
	}
	
	public List<AudioFileModel> getAudioFilesModels() {
		return audioFilesModels;
	}
	public void setAudioFilesModels(List<AudioFileModel> audioFilesModels) {
		playlist.getAudioFiles().clear();
		
		for (AudioFileModel iterator : audioFilesModels) {
			playlist.getAudioFiles().add(iterator.getAudioFile());
		}
		
		this.audioFilesModels = audioFilesModels;
		
		setChanged();
		notifyObservers("list");
	}
	

	public void update(Observable arg0, Object arg1) {
		setChanged();
		notifyObservers(arg1);
	}
	
	public String toString() {
		return playlist.toString();
	}
	
	public void addAudioFileToPlaylist(AudioFileModel audioFileModel) {
		playlist.getAudioFiles().add(audioFileModel.getAudioFile());
		audioFilesModels.add(audioFileModel);
		
		setChanged();
		notifyObservers("list");
	}
}
